package com.gamatechno.solodestination;

import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.gamatechno.adapter.ImageLoader;
import com.gamatechno.solodestination.model.AppPreferences;
import com.gamatechno.solodestination.model.HttpUtils;
import com.gamatechno.solodestination.model.Komentar;

import android.R.bool;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class PostKomentarActivity extends Activity {

	// http://sms.gamatechno.com/solo_destination/services/getKuliner.php?method=rate&user_id=1&kuliner_id=5&format=json&rating=5&comment=Keren
	String id_kuliner = "";
	Button btn_post_komentar;
	ImageView iv_pp;
	EditText et_comment;
	RatingBar rating;
	AppPreferences _pref;
	String idFb = "0";
	ImageLoader imgloader;
	String query;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_komentar);
		imgloader = new ImageLoader(this);
		_pref = new AppPreferences(this);
		idFb = _pref.getIdFb();
		btn_post_komentar = (Button) findViewById(R.id.btn_post_komentar);
		iv_pp = (ImageView) findViewById(R.id.iv_pp);
		et_comment = (EditText) findViewById(R.id.et_komentar);
		
		rating = (RatingBar) findViewById(R.id.ratingBar1);
		Bundle extras = getIntent().getExtras();
		
		
		
		
		
		if (extras == null) {
			return;
		}
		// get data via the key
		String value1 = extras.getString("id_kuliner");
		if (value1 != null) {
			// do something with the data
			id_kuliner = value1;
		}
		Log.wtf("id_kuliner", value1);
		Log.wtf("idFb", idFb);
		btn_post_komentar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.wtf("btn_post_komentar", "clicked");
				try {
					query = URLEncoder.encode(et_comment.getText().toString(), "utf-8");
					
				} catch (Exception e) {
					Log.i("error_solo", e.getMessage());
					
				}
				
				Log.i("error_solo", query);
				new PostKomentar().execute();
			}
		});

		if (!idFb.equals("")) {
			// et_comment.setText("idfb: "+idFb);
		}
//		Log.wtf("DisplayImage", "http://graph.facebook.com/" + idFb
//				+ "/picture");
//		imgloader.DisplayImage(
//				"http://graph.facebook.com/" + idFb + "/picture", iv_pp);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.post_komentar, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class PostKomentar extends AsyncTask<Void, Void, Boolean> {

		// ProgressDialog dialog = new ProgressDialog(Tab.this);

		protected void onPreExecute() {
			// dialog.setMessage(" Loading... ");
			// dialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {

			Boolean result = false;
			String url = "http://sms.gamatechno.com/solo_destination/services/getKuliner.php?method=rate&user_id="
					+ idFb
					+ "&kuliner_id="
					+ id_kuliner
					+ "&format=json&rating="
					+ rating.getRating()
					+ "&comment="
					+ query;

			Log.i("url", url);
			try {
				HttpUtils u = new HttpUtils();
				String a = u.getContents(url);
				Log.i("content", a);
				JSONObject obj = new JSONObject(a);
				Log.i("JSONObject content length", obj.length() + "");
				String jo = obj.get("result") + "";
				Log.i("isi result", jo + "");
				JSONObject ka = new JSONObject(jo);
				Log.i("result length", ka.length() + "");
				Log.i("result msg", ka.getString("msg") + "");
				if (ka.getString("msg").equals("true")) {
					result = true;
				}
			} catch (Exception e) {
				// e.printStackTrace();
				Log.i("i", e.toString());
			}

			return result;

		}

		@Override
		protected void onPostExecute(Boolean result) {
			Toast.makeText(PostKomentarActivity.this, result + "",
					Toast.LENGTH_SHORT).show();
			if (result) {
				setResult(RESULT_OK, null);
				finish();
			} else {
				Toast.makeText(PostKomentarActivity.this, result + "",
						Toast.LENGTH_SHORT).show();
			}
		}

	}

}
